#!/usr/bin/python
# -*-coding:utf8-*-

import time
timeVolue = 20
from UiManager.utils.common import hooks


class ElementOpration(object):
    def __init__(self, driver=None):
        self.driver = driver

    def get_window_size(self):
        x = self.driver.get_window_size()['width']
        y = self.driver.get_window_size()['height']
        return x, y

    def find_by_ui_opration(self, location_type=None, element_value=None, opration_name=None, input_data=None):
        flag = False
        x, y = self.get_window_size()

        if opration_name == "click":
            for i in range(timeVolue):
                try:
                    self.driver.find_element_by_android_uiautomator('new UiSelector().%s("%s")' % (location_type, element_value)).click()
                    time.sleep(2)
                    flag = True
                    break
                except:
                    print('new UiSelector().%s("%s")' % (location_type, element_value))
                    time.sleep(2)

        elif opration_name == "function":
            hooks(input_data, self.driver)

        elif opration_name == "input":
            if input_data and input_data[0] == "$":
                input_data = hooks(input_data, self.driver)
            for i in range(timeVolue):
                try:
                    self.driver.find_element_by_android_uiautomator(
                        'new UiSelector().%s("%s")' % (location_type, element_value)).send_keys(input_data)
                    time.sleep(2)
                    flag = True
                    break
                except:
                    print('new UiSelector().%s("%s")' % (location_type, element_value))
                    time.sleep(2)

        elif opration_name == "check":
            for i in range(timeVolue):
                try:
                    self.driver.find_element_by_android_uiautomator(
                        'new UiSelector().%s("%s")' % (location_type, element_value))
                    flag = True
                    break
                except Exception:
                    print('new UiSelector().%s("%s")' % (location_type, element_value))
                    time.sleep(2)

        elif opration_name == "swipLeft":
            for i in range(int(input_data)):
                time.sleep(3)
                try:
                    self.driver.swipe(x * 8 / 9, y / 2, x / 10, y / 2, 1000)
                except Exception as e:
                    print(e)

        elif opration_name == "swipRight":
            for i in range(int(input_data)):
                time.sleep(2)
                try:
                    self.driver.swipe(x / 10, y / 2, x * 8 / 9, y / 2, 1000)
                except Exception as e:
                    print(e)

        elif opration_name == "swipUp":
            for i in range(int(input_data)):
                time.sleep(1)
                try:
                    self.driver.swipe(x / 2, y * 3 / 4, x / 2, y / 4, 1000)
                except Exception as e:
                    print(e)

        elif opration_name == "swipDown":
            for i in range(int(input_data)):
                time.sleep(1)
                try:
                    self.driver.swipe(x / 2, y / 4, x / 2, y * 3 / 4, 1000)
                except Exception as e:
                    print(e)

        elif opration_name == "tap":
            try:
                time.sleep(2)
                x = input_data.replace('，', ',').split(',')[0].strip()
                y = input_data.replace('，', ',').split(',')[1].strip()
                self.driver.tap([(x, y)])
            except Exception as e:
                print(e)

        return flag

